package person.aoao.library.controller;

import person.aoao.library.entity.BorrowList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BorrowListService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (BorrowList)表控制层
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@RestController
@RequestMapping("borrowList")
@CrossOrigin
public class BorrowListController {
    /**
     * 服务对象
     */
    @Resource
    private BorrowListService borrowListService;

    /**
     * 分页查询
     *
     * @param borrowList  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<BorrowList>> queryByPage(BorrowList borrowList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.borrowListService.queryByPage(borrowList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<BorrowList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.borrowListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param borrowList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<BorrowList> add(BorrowList borrowList) {
        return ResponseEntity.ok(this.borrowListService.insert(borrowList));
    }

    /**
     * 编辑数据
     *
     * @param borrowList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<BorrowList> edit(BorrowList borrowList) {
        return ResponseEntity.ok(this.borrowListService.update(borrowList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.borrowListService.deleteById(id));
    }


    //通过uid获取借阅记录
    @GetMapping("getBorrowList/{uid}/{num}/{page}")
    public ResponseData getBorrowList(@PathVariable String uid,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.borrowListService.queryByUid(uid,num,page);
        return data;
    }

    @GetMapping("getBorrowListAll/{num}/{page}")
    public ResponseData getBorriwListAll(@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.borrowListService.selectAll(num,page);
        return  data;
    }

    @PostMapping("updateBorrowOne")
    public ResponseData updateBorrowOne(@RequestBody BorrowList borrowList){
        BorrowList list=this.borrowListService.update(borrowList);
        return  new ResponseData(ResponseCode.SUCCESS,list);
    }

    @PostMapping("deleteBorrowOne")
    public ResponseData deleteBorrowOne(@RequestBody BorrowList borrowList){
        this.borrowListService.deleteById(borrowList.getBorrowid());
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @PostMapping("searchBorrow/{state}/{num}/{page}")
    public ResponseData searchBorrow(@RequestBody String keyWord,@PathVariable Integer state,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.borrowListService.searchBorrow(keyWord.replace("\"", ""),state,num,page);
        return data;
    }
}

